package com.x.ant.dao;

import com.x.ant.model.Bill;
import com.x.ant.model.BillTruck;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class BillDao extends BaseDao {

    private final static String selectBillListSql = "SELECT T1.* FROM ANT_BILL T1, ANT_TASK T2 WHERE T1.TASK_ID = T2.TASK_ID ";
    public List<Bill> selectBillList(Bill bill) throws Exception{
        StringBuilder sb = new StringBuilder(selectBillListSql);
        List params = new ArrayList();
        if (this.isString(bill.getBillId())) {
            sb.append("AND T1.BILL_ID = ? ");
            params.add(bill.getBillId());
        }
        if (this.isString(bill.getTaskId())) {
            sb.append("AND T1.TASK_ID = ? ");
            params.add(bill.getTaskId());
        }
        if (this.isString(bill.getDeptId())) {
            sb.append("AND T1.DEPT_ID = ? ");
            params.add(bill.getDeptId());
        }
        if (bill.getBillState() != null) {
            sb.append("AND T1.BILL_STATE = ? ");
            params.add(bill.getBillState());
        }
        sb.append("ORDER BY T1.BILL_CREATE_TIME DESC ");
        return this.queryForList(sb.toString(), Bill.class, params);
    }

    private final static String selectBillNumSql = "SELECT COUNT(1) FROM ANT_BILL T1, ANT_TASK T2 WHERE T1.TASK_ID = T2.TASK_ID ";
    public int selectBillNum(Bill bill) throws Exception{
        StringBuilder sb = new StringBuilder(selectBillNumSql);
        List params = new ArrayList();
        if (this.isString(bill.getBillId())) {
            sb.append("AND T1.BILL_ID = ? ");
            params.add(bill.getBillId());
        }
        if (this.isString(bill.getTaskId())) {
            sb.append("AND T1.TASK_ID = ? ");
            params.add(bill.getTaskId());
        }
        if (this.isString(bill.getDeptId())) {
            sb.append("AND T1.DEPT_ID = ? ");
            params.add(bill.getDeptId());
        }
        if (bill.getBillState() != null) {
            sb.append("AND T1.BILL_STATE = ? ");
            params.add(bill.getBillState());
        }
        if(bill.getBillCreateTime() != null){
            sb.append("AND DATE(T1.BILL_CREATE_TIME) = ? ");
            params.add(bill.getBillCreateTime());
        }
        return this.queryForInt(sb.toString(), params);
    }

    private final static String selectBillTruckListSql = "SELECT T1.BILL_ID, T1.BILL_TRUCK_LOAD, T1.BILL_TRUCK_STATE, " +
            "T2.*, T3.USER_NAME, T3.USER_MOBILE, T4.BILL_CODE " +
            "FROM ANT_BILL_TRUCK T1, ANT_TRUCK T2, ANT_USER T3, ANT_BILL T4 " +
            "WHERE T1.TRUCK_ID = T2.TRUCK_ID " +
            "AND T1.BILL_ID = T4.BILL_ID " +
            "AND T2.USER_ID = T3.USER_ID ";
    public List<BillTruck> selectBillTruckList(BillTruck billTruck) throws Exception{
        StringBuilder sb = new StringBuilder(selectBillTruckListSql);
        List params = new ArrayList();
        if (this.isString(billTruck.getBillId())) {
            sb.append("AND T1.BILL_ID = ? ");
            params.add(billTruck.getBillId());
        }
        if (billTruck.getBillTruckState() != null) {
            sb.append("AND T1.BILL_TRUCK_STATE = ? ");
            params.add(billTruck.getBillTruckState());
        }
        if(billTruck.getTruck() != null && this.isString(billTruck.getTruck().getTruckPlateHeader())) {
            sb.append("AND T2.TRUCK_PLATE_HEADER = ? ");
            params.add(billTruck.getTruck().getTruckPlateHeader());
        }
        if(billTruck.getTruck() != null && this.isString(billTruck.getTruck().getTruckPlateMiddle())) {
            sb.append("AND T2.TRUCK_PLATE_MIDDLE = ? ");
            params.add(billTruck.getTruck().getTruckPlateMiddle());
        }
        if(billTruck.getTruck() != null && this.isString(billTruck.getTruck().getTruckPlateCode())) {
            sb.append("AND T2.TRUCK_PLATE_CODE = ? ");
            params.add(billTruck.getTruck().getTruckPlateCode());
        }
        sb.append("ORDER BY T1.BILL_TRUCK_CREATE_TIME DESC ");
        return this.queryForList(sb.toString(), BillTruck.class, params);
    }

//    private static String updateBillSql = "update ANT_BILL set BILL_TRUCK_NUM = BILL_TRUCK_NUM + ? where BILL_ID = ?";
//    public void updateBillTruckNum(Bill bill) {
//        Object[] params = {bill.getBillTruckNum(), bill.getBillId()};
//        this.execute(updateBillSql, params);
//    }

}
